package com.common.www.bean.entity.sale;

//import com.bianmaba.realestate.bean.entity.authorization.UserRoleMiddle;

import com.common.www.MyApplicationListener;
import org.springframework.boot.base.jpa.domain.annotations.QueryCacheable;
import org.springframework.boot.base.jpa.domain.entity.NumberIdentityEntity;
import com.common.www.bean.entity.auth.AuthUser;
import com.common.www.bean.entity.recycle.RecycleBill;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;

import java.math.BigDecimal;
import java.util.List;

import javax.persistence.*;

import io.swagger.annotations.ApiModel;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

/**
 * 卖出单对象
 *
 * @author luozisong
 */
@DynamicUpdate(true)
@DynamicInsert(true)
@Entity
@Getter
@Setter
@QueryCacheable
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table()
@Builder
@AllArgsConstructor
@NoArgsConstructor
@ApiModel(value = "project返回参数")
public class SaleBill extends NumberIdentityEntity {

    @JsonIgnoreProperties(value = {"authRoles", "company"})
    @ManyToOne()
    private AuthUser createUser;

    /**
     * 当前会所属的品类
     */
    @JsonIgnoreProperties(value = {"saleBill"})
    //二级分类
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "saleBill")
    private List<SaleBillTypeItem> typeList;
    /**
     * 关联的收货单
     */
    @JsonIgnoreProperties(value = {"typeList", "saleBill"})
    //二级分类
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "saleBill")
    private List<RecycleBill> recycleBillList;

    /**
     * 当前会所属的品类
     */
    @JsonIgnoreProperties(value = {"bill"})
    //二级分类
    @OneToMany(cascade = CascadeType.ALL, orphanRemoval = true, mappedBy = "bill")
    private List<SaleBillImgItem> imgItemList;

    /**
     * 关联的卖货工厂
     */
    @JsonIgnoreProperties(value = {"typeList"})
    @ManyToOne()
    @JoinColumn(name = "parent")
    private SaleFactory saleFactory;

    /**
     * 状态 1.已完成称重周结算 2.已完成称重月结算 3.已完成称重年结算 10.已完成并支付完成到账（线下立即支付）
     */
    @Column(length = 11)
    private Integer payType;

    /**
     * 定时结算状态 1.周结算未结算 2.月结算未结算 3.年结算未结算   101.周结算已结算 102.月结算已结算 103.年结算已结算
     */
    @Column(length = 11)
    private Integer periodStatus;

    /**
     * 实际卖出总金额
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal priceActual;
    /**
     * 实际称的总重
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal weightActual;


    /**
     * 收货单的总金额
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal priceRecycleTotal;
    /**
     * 收货单的总重
     */
    @Column(precision = 23, scale = 2)
    private BigDecimal weightRecycleTotal;

    /**
     * 签字图片OSS的对象名称
     */
    @Column(length = 128)
    private String signImg;
    /**
     * 备注
     */
    @Column(length = 11)
    private String remark;

    @Transient
    private String statusName;

    public String getStatusName() {
        if (payType == null) {
            return "未知";
        }
        if (payType == 1) {
            return "周结";
        } else if (payType == 2) {
            return "月结";
        } else if (payType == 3) {
            return "年结";
        } else if (payType == 10) {
            return "已完成";
        } else {
            return "未知";
        }
    }


    public String getSignImgUrl() {
        return MyApplicationListener.projectPath + signImg;
    }
}